<!DOCTYPE html>
<html>
	<head>
	</head>
	<body>
		<script>
			
			var _gaq = _gaq || [];
			_gaq.push(['_setAccount', 'UA-12829274-3']);
			_gaq.push(['_trackPageview']);

			(function() {
			  var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
			  ga.src = 'https://ssl.google-analytics.com/ga.js';
			  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
			})();
			
			function sayHello(username, callback) {
				var xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function(data) {
					if (xhr.readyState == 4) {
						if (xhr.status == 200) {
							var data = JSON.parse(xhr.responseText);
							callback(data);
						} else {
							callback(null);
						}
					}
				}
				// Note that any URL fetched here must be matched by a permission in
				// the manifest.json file!
				var url = 'http://www.nov8rix.com/services/cpipeline.svc/DCT/' + username + '/Hello';
				xhr.open('GET', url, true);
				xhr.send();
			};
			
			function addReview(userId, cid, body, callback) {
				var xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function(data) {
					if (xhr.readyState == 4) {
						if (xhr.status == 200) {
							callback(true);
						} else {
							callback(null);
						}
					}
				}
				
				// Note that any URL fetched here must be matched by a permission in
				// the manifest.json file!
				var url = 'http://kdice.com/profile/' +  userId + '/addReview';
				xhr.open('POST', url, true);
				
				var advert = " -- [This review was automatically posted using Deadcode Tools for KDice.  Download: http://kdice.com/profile/44773121]"
				var params = "prev=/profile/" + userId + "/reviews&cid=&body=" + body + advert;
				
				//Send the proper header information along with the request
				xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xhr.setRequestHeader("Content-length", params.length);
				xhr.setRequestHeader("Connection", "keep-alive");
				
				xhr.send(params);
			};

			/**
			* Handles data sent via chrome.extension.sendRequest().
			* @param request Object Data sent in the request.
			* @param sender Object Origin of the request.
			* @param callback Function The method to call when the request completes.
			*/
			function onRequest(request, sender, callback) {
				if (request.action == 'sayHello') {
					sayHello(request.username, callback);
				} else if (request.action == 'addReview') {
					addReview(request.userId, request.cid, request.review, callback);
				} else if (request.action == "trackEvent") {
					_gaq.push(['_trackEvent', request.category, request.evnt ]);
				}
			};

			// Wire up the listener.
			chrome.extension.onRequest.addListener(onRequest);
		</script>
	</body>
</html>